﻿Imports Entities
Public Class BoletaDAL
    Public Shared Function Create(ByVal zObject As BOLETAS) As BOLETAS
        Try
            Using bd As New gas_gasdesuvecinoEntities
                bd.boletas.Add(zObject)
                bd.SaveChanges()
                Return zObject
            End Using
        Catch ex As Exception
            Debug.Print("BoletaDAL.Create() " + ex.Message)
            Return Nothing
        End Try
    End Function
    Public Shared Function Update(ByVal zObject As BOLETAS) As BOLETAS
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = (From rec In bd.BOLETAS
                          Where rec.BOLETA = zObject.BOLETA
                          Select rec).SingleOrDefault


                query.TOTAL = zObject.TOTAL
                query.MONTONETO = zObject.MONTONETO
                query.DESCUENTO = zObject.DESCUENTO
                query.DESCRIPCION = zObject.DESCRIPCION

                bd.SaveChanges()
                Return zObject
            End Using
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Shared Function Delete(ByVal zBoletaId As Long) As Boolean
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = (From rec In bd.BOLETAS
                          Where rec.BOLETA = zBoletaId
                          Select rec).SingleOrDefault

                bd.BOLETAS.Remove(query)
                bd.SaveChanges()
                Return True
            End Using
        Catch ex As Exception
            Return False
        End Try
    End Function

    Public Shared Function List() As List(Of boletas)
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Return bd.BOLETAS.ToList
            End Using
        Catch ex As Exception
            Debug.Print("BoletaDAL.List() " + ex.Message)
            Return Nothing
        End Try
    End Function
   

    'Public Shared Function SearchByPagoId(ByVal zId As Long) As BOLETAS
    '    Try
    '        Using bd As New gas_gasdesuvecinoEntities
    '            Dim query = From rec In bd.BOLETAS
    '                        Where rec.PAGO = zId

    '            Return query.SingleOrDefault
    '        End Using
    '    Catch ex As Exception
    '        Debug.Print("BoletaDAL.SearchById() " + ex.Message)
    '        Return Nothing
    '    End Try
    'End Function
    Public Shared Function SearchById(ByVal zId As Long) As boletas
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Return bd.BOLETAS.Find(zId)
            End Using
        Catch ex As Exception
            Debug.Print("BoletaDAL.SearchById() " + ex.Message)
            Return Nothing
        End Try
    End Function
    Public Shared Function GetLast() As boletas
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = (From rec In bd.BOLETAS
                             Order By rec.BOLETA Descending
                          Select rec).Take(1).SingleOrDefault
                Return (query)
            End Using
        Catch ex As Exception
            Debug.Print("BoletaDAL.GetLast() " + ex.Message)
            Return Nothing
        End Try
    End Function
End Class
